﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraPrinting;
using DevExpress.XtraEditors;

namespace MINERP.ComControls.Extension
{
    public static class DevControlExtension
    {


        public static void ExportToExcel(this DevExpress.XtraGrid.GridControl gridControl, IWin32Window owner, string inFileName = null)
        {
            SaveFileDialog fileDialog = new SaveFileDialog();
            fileDialog.Title = "导出Excel";
            fileDialog.Filter = "Excel文件(*.xls)|*.xls";
            if (!string.IsNullOrEmpty(inFileName))
            {
                fileDialog.FileName = inFileName;
            }
            DialogResult dialogResult = fileDialog.ShowDialog(owner);
            if (dialogResult == DialogResult.OK)
            {
                DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                //options.ExportMode = XlsExportMode.SingleFile;
                gridControl.ExportToXls(fileDialog.FileName);
                DevExpress.XtraEditors.XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        public static void ShowRowIndicator(this DevExpress.XtraGrid.Views.Grid.GridView gv, int width)
        {
            gv.CustomDrawRowIndicator += new DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventHandler(gv_CustomDrawRowIndicator);
            if (width != null)
            {
                if (width != 0)
                {
                    gv.IndicatorWidth = width;
                }
                else
                {
                    gv.IndicatorWidth = 30;
                }
            }
            else
            {
                gv.IndicatorWidth = 30;
            }

        }
        private static void gv_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator && e.RowHandle > -1)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }

        //public static void FillEnum<T>(this ComboBoxEdit comboBox, Func<int, bool> condition = null, bool addAllItem = false)
        //{
        //    EnumItem[] arr = typeof(T).ConvertEnumItem();
        //    if (condition != null)
        //    {
        //        arr = arr.Where(c => condition(c.Value)).ToArray();
        //    }
        //    comboBox.Properties.BeginUpdate();
        //    if (addAllItem)
        //    {
        //        comboBox.Properties.Items.Add(new EnumItem(-1, "(全部)"));
        //    }
        //    comboBox.Properties.Items.AddRange(arr);
        //    comboBox.Properties.EndUpdate();
        //}
    }
}
